package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketAdService;
import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class MarketAdServiceImpl implements MarketAdService {
    @Autowired
    private MarketAdMapper adMapper;
    @Override
    public List<MarketAd> adSelect(String name, String content, String sort, String order,Integer limit,Integer page) {
        Integer currentPage = page;
        Integer pagesize = limit;
        MarketAdExample marketAdExample = new MarketAdExample();
        if (name == null && content == null) {
            //不输入内容，查询整个表单
            // select * from marketAd
            marketAdExample.orderBy(sort + " " + order);
            marketAdExample.or().andIdIsNotNull().andDeletedNotEqualTo(true);
            PageHelper.startPage(currentPage, pagesize);
            List<MarketAd> marketAd = adMapper.selectByExample(marketAdExample);

            return marketAd;
        }

        if (name == null && content != null) {
            marketAdExample.or().andContentLike("%" + content + "%").andDeletedNotEqualTo(true);;
            PageHelper.startPage(currentPage, pagesize);
            List<MarketAd> marketAd = adMapper.selectByExample(marketAdExample);

            return marketAd;
        }

        if (name != null && content == null) {
            marketAdExample.or().andContentLike("%" + name + "%").andDeletedNotEqualTo(true);;
            PageHelper.startPage(currentPage, pagesize);
            List<MarketAd> marketAd = adMapper.selectByExample(marketAdExample);

            return marketAd;
        }


        marketAdExample.or().andNameLike("%" + name + "%").andContentLike("%" + content + "%").andDeletedNotEqualTo(true);;
        PageHelper.startPage(currentPage, pagesize);
        List<MarketAd> marketAd = adMapper.selectByExample(marketAdExample);

        return marketAd;
    }

    @Override
    public void deleteAd(String id) {
        
        MarketAd marketAd = new MarketAd();
        marketAd.setDeleted(true);
        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.or().andIdEqualTo(Integer.valueOf(id));
        adMapper.updateByExampleSelective(marketAd,marketAdExample);
        
        
    }

    @Override
    public List<MarketAd> adUpdate(String id, String name, String link, String url, String position, String content, String enable, String updateTime, String deleted) {
        Byte position1 = 1;
        boolean enable1 = true;
        boolean deleted1 = false;
        LocalDateTime now = LocalDateTime.now();
        if (position.equals(true)) {
            position1 = 1;
        }
        if (position.equals(false)) {
            position1 = 0;
        }
        if (enable.equals(true)) {
            enable1 = true;
        }
        if (enable.equals(false)) {
            enable1 = false;
        }
        if (deleted.equals(true)) {
            deleted1 = true;
        }
        if (deleted.equals(false)) {
            deleted1 = false;
        }
        
        MarketAd marketAd = new MarketAd();
        marketAd.setName(name);
        marketAd.setLink(link);
        marketAd.setUrl(url);
        marketAd.setPosition(position1);
        marketAd.setContent(content);
        marketAd.setEnabled(enable1);
        marketAd.setUpdateTime(now);
        marketAd.setDeleted(deleted1);
        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.or().andIdEqualTo(Integer.valueOf(id));
        adMapper.updateByExampleSelective(marketAd,marketAdExample);
        
        MarketAd marketAd1 = adMapper.selectByPrimaryKey(Integer.valueOf(id));
        List<MarketAd> marketAds = new ArrayList<>();
        marketAds.add(marketAd1);
        
        
        return marketAds;
    }

    @Override
    public MarketAd adCreate(String content, String enabled, String link, String name, String position, String url) {
        MarketAd marketAd = new MarketAd();
        marketAd.setName(name);
        marketAd.setLink(link);
        marketAd.setUrl(url);
        marketAd.setPosition(Byte.valueOf(position));
        marketAd.setContent(content);
        marketAd.setEnabled(Boolean.valueOf(enabled));
        marketAd.setAddTime(LocalDateTime.now());
        marketAd.setUpdateTime(LocalDateTime.now());
        marketAd.setDeleted(false);
        
        adMapper.insertSelective(marketAd);
        
        
        return marketAd;
    }
}

